

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

public class SungJukV2Client extends JFrame {

	private JPanel contentPane;
	private JTextField name;
	private JTextField kor;
	private JTextField eng;
	private JTextField mat;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					SungJukV2Client frame = new SungJukV2Client();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public SungJukV2Client() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JLabel title = new JLabel(":: 성적 처리 프로그램 v1 ::");
		
		JLabel lblName = new JLabel("이  름");
		
		JLabel lblKor = new JLabel("국  어");
		
		JLabel lblEng = new JLabel("영  어");
		
		name = new JTextField();
		name.setColumns(10);
		
		kor = new JTextField();
		kor.setColumns(10);
		
		eng = new JTextField();
		eng.setColumns(10);
		
		JLabel lblMat = new JLabel("수  학");
		
		mat = new JTextField();
		mat.setColumns(10);
		
		JButton saveBtn = new JButton("입 력 완 료");
		saveBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 입력 받은 성적 데이터를 SungJukV2VO에 저장
				SungJukV2VO sj = new SungJukV2VO(name.getText(), Integer.parseInt(kor.getText()), Integer.parseInt(eng.getText()), Integer.parseInt(mat.getText()));
				
				// SungJukV2Server에 접속하고
				// SungJukV2VO 객체를 서버에 전송
				try{
					System.out.println("[클라이언트] 서베에 접속을 시도합니다...");
					Socket connection = new Socket("124.124.124.223",9563);
					
					ObjectInputStream ois = null;
					ObjectOutputStream oos = null;
					
					// 서버로 성적 객체를 전송함
					oos = new ObjectOutputStream(connection.getOutputStream());
					oos.writeObject(sj);
					
					// 서버에서 성적객체를 받아옴
					ois = new ObjectInputStream(connection.getInputStream());
					sj = (SungJukV2VO)ois.readObject();
					
				}catch(Exception ex){
					ex.printStackTrace();
				}
				
				JOptionPane.showMessageDialog(null, sj.toString());
			}
		});
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(138)
					.addComponent(title)
					.addContainerGap(142, Short.MAX_VALUE))
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(73)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addComponent(lblName)
						.addComponent(lblKor)
						.addComponent(lblEng)
						.addComponent(lblMat))
					.addPreferredGap(ComponentPlacement.RELATED, 142, Short.MAX_VALUE)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addComponent(mat, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(name, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(kor, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(eng, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(61, Short.MAX_VALUE))
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(163)
					.addComponent(saveBtn)
					.addContainerGap(168, Short.MAX_VALUE))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addComponent(title)
					.addGap(38)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblName)
						.addComponent(name, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblKor)
						.addComponent(kor, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblEng)
						.addComponent(eng, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblMat)
						.addComponent(mat, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addComponent(saveBtn)
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		contentPane.setLayout(gl_contentPane);
	}
}
